import { Arduino } from 'blockly/core';


export const mixly_lib_fs_begin = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${FIELD_FS}.${FIELD_TYPE}()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_open = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH = Arduino.valueToCode(this, 'PATH', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${FIELD_FS}.open(${VALUE_INPUT_PATH}, "${FIELD_TYPE}")`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_open_dir = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH = Arduino.valueToCode(this, 'PATH', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    return [`${FIELD_FS}.openDir(${VALUE_INPUT_PATH})`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_exists = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH = Arduino.valueToCode(this, 'PATH', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    return [`${FIELD_FS}.exists(${VALUE_INPUT_PATH})`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_op_dir = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH = Arduino.valueToCode(this, 'PATH', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return `${FIELD_FS}.${FIELD_TYPE}(${VALUE_INPUT_PATH});\n`;
}

export const mixly_lib_fs_remove = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH = Arduino.valueToCode(this, 'PATH', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    return `${FIELD_FS}.remove(${VALUE_INPUT_PATH});\n`;
}

export const mixly_lib_fs_rename = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_PATH1 = Arduino.valueToCode(this, 'PATH1', Arduino.ORDER_ATOMIC);
    const VALUE_INPUT_PATH2 = Arduino.valueToCode(this, 'PATH2', Arduino.ORDER_ATOMIC);
    const FIELD_FS = this.getFieldValue('FS');
    if (FIELD_FS === 'LittleFS') {
        Arduino.definitions_['include_LittleFS'] = '#include <LittleFS.h>';
    }
    return `${FIELD_FS}.rename(${VALUE_INPUT_PATH1}, ${VALUE_INPUT_PATH2});\n`;
}

export const mixly_lib_fs_while_next = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_DIR = Arduino.valueToCode(this, 'DIR', Arduino.ORDER_ATOMIC) || 'dir';
    const STATEMENT_INPUT_DO = Arduino.statementToCode(this, 'DO');
    return `while (${VALUE_INPUT_DIR}.next()) {\n`
        + (STATEMENT_INPUT_DO ? `${STATEMENT_INPUT_DO}\n` : '')
        +'}\n'
        + `${VALUE_INPUT_DIR}.rewind();\n`;
}

export const mixly_lib_fs_get_dir_info = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_DIR = Arduino.valueToCode(this, 'DIR', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${VALUE_INPUT_DIR}.${FIELD_TYPE}()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_open_file = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_DIR = Arduino.valueToCode(this, 'DIR', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${VALUE_INPUT_DIR}.openFile("${FIELD_TYPE}")`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_get_file_info = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${VALUE_INPUT_FILE}.${FIELD_TYPE}()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_stat = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_DIR = Arduino.valueToCode(this, 'DIR', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${VALUE_INPUT_DIR}.${FIELD_TYPE}()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_file_read = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return [`${VALUE_INPUT_FILE}.${FIELD_TYPE}()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_file_read_string = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    return [`${VALUE_INPUT_FILE}.readString()`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_file_read_string_until = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    const VALUE_INPUT_UNTIL = Arduino.valueToCode(this, 'UNTIL', Arduino.ORDER_ATOMIC);
    return [`${VALUE_INPUT_FILE}.readStringUntil(${VALUE_INPUT_UNTIL})`, Arduino.ORDER_ATOMIC];
}

export const mixly_lib_fs_file_print = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    const VALUE_INPUT_VALUE = Arduino.valueToCode(this, 'VALUE', Arduino.ORDER_ATOMIC);
    const FIELD_TYPE = this.getFieldValue('TYPE');
    return `${VALUE_INPUT_FILE}.${FIELD_TYPE}(${VALUE_INPUT_VALUE});\n`;
}

export const mixly_lib_fs_file_close = function () {
    Arduino.definitions_['include_FS'] = '#include <FS.h>';
    const VALUE_INPUT_FILE = Arduino.valueToCode(this, 'FILE', Arduino.ORDER_ATOMIC);
    return `${VALUE_INPUT_FILE}.close();\n`;
}